package com.reliable.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reliable.common.annotation.DataSource;
import com.reliable.common.enums.DataSourceType;
import com.reliable.service.domain.ReliabUploadRecord;
import com.reliable.service.mapper.ReliabUploadRecordMapper;
import com.reliable.service.service.IReliabUploadRecordService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * Excel文件上传任务记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-17
 */
@Service
@DataSource(value = DataSourceType.MASTER)
public class ReliabUploadRecordServiceImpl extends ServiceImpl<ReliabUploadRecordMapper, ReliabUploadRecord> implements IReliabUploadRecordService {

    @Autowired
    private ReliabUploadRecordMapper uploadRecordMapper;

    /**
     * 更新上传记录状态（成功/失败/部分成功）
     *
     * @param uploadId     上传记录ID
     * @param totalRows    总行数（可为 null）
     * @param successRows  成功行数（可为 null）
     * @param failedRows   失败行数（可为 null）
     * @param status       状态：1=处理中, 2=成功, 3=失败, 4=部分成功
     * @param errorMsg     错误信息（可为 null）
     */
    @DataSource(value = DataSourceType.MASTER)
    public void updateUploadRecord(Long uploadId, Integer totalRows, Integer successRows, Integer failedRows, Byte status, String errorMsg) {
        ReliabUploadRecord record = new ReliabUploadRecord();
        record.setId(uploadId);
        record.setTotalRows(totalRows);
        record.setSuccessRows(successRows);
        record.setFailedRows(failedRows);
        record.setStatus(Integer.valueOf(status));
        record.setErrorMessage(errorMsg);
        record.setUpdateTime(new Date());
        uploadRecordMapper.updateById(record);
    }

    @Override
    @DataSource(value = DataSourceType.MASTER)
    public void updateUploadRecordSuccess(Long uploadId, int totalRows, int successRows, int failedRows) {
        byte status = (failedRows == 0) ? (byte) 2 : (byte) 4;
        updateUploadRecord(uploadId, totalRows, successRows, failedRows, status, null);
    }

    @Override
    @DataSource(value = DataSourceType.MASTER)
    public void updateUploadRecordFailed(Long uploadId, String errorMsg) {
        updateUploadRecord(uploadId, null, null, null, (byte) 3, errorMsg);
    }

    public void updateOssUrl(Long id, String ossUrl) {
        ReliabUploadRecord record = new ReliabUploadRecord();
        record.setId(id);
        record.setOssUrl(ossUrl);
        this.updateById(record);
    }


    public Long getUploadIdByTaskId(Long taskId) {
        LambdaQueryWrapper<ReliabUploadRecord> query = new LambdaQueryWrapper<>();
        query.eq(ReliabUploadRecord::getTaskId, taskId)
                .orderByDesc(ReliabUploadRecord::getCreateTime)
                .last("LIMIT 1");
        ReliabUploadRecord record = this.getOne(query);
        return record != null ? record.getId() : null;
    }
}